#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<algorithm>
#include<vector>

using namespace std;


int main()

{

	int T, M;

	int a, b, i, j;

	int ret = 0;

	scanf("%d%d", &T, &M);

	int t[1001], v[101];


	vector<vector<int>> dp(M+1, vector<int>(T+1 , 0));



	for (i = 1; i <= M; ++i)

	{

		scanf("%d%d", &a, &b);

		t[i]=a;

		v[i]=b;

	}
	for(i=1;i<=M;++i)
	for (j = T; j >= 1; --j)

	{

		if (j >= t[i])
		{
			dp[i][j] = max(dp[i - 1][j - t[i]] + v[i], dp[i - 1][j]);
		}
		else
		{
			dp[i][j] = dp[i - 1][j];
		}

	}

	

	printf("%d", dp[M][T]);

}

